/**
 * 用户信息状态模块
 */
import auth from '@/utils/auth'

const state = {
  // 用户信息
  user: {
    name: '张医生',
    position: '主治医师',
    department: '神经外科',
    avatar: 'https://randomuser.me/api/portraits/men/85.jpg',
    id: 'U10001',
    email: 'zhangys@hplus.com',
    mobile: '13800138000'
  },
  // 用户统计
  stats: {
    todo: 12,
    assets: 5,
    requests: 3
  },
  // 权限列表
  permissions: ['asset:view', 'asset:apply', 'finance:view', 'todo:view', 'todo:approve'],
  // 登录状态
  token: auth.getToken()
}

const mutations = {
  // 设置用户信息
  SET_USER_INFO: (state, userInfo) => {
    state.user = userInfo
  },
  // 设置用户统计
  SET_USER_STATS: (state, stats) => {
    state.stats = stats
  },
  // 设置权限列表
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  },
  // 设置令牌
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  // 重置状态
  RESET_STATE: (state) => {
    state.user = {}
    state.stats = {}
    state.permissions = []
    state.token = ''
  }
}

const actions = {
  // 登录
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    
    // 这里应当进行实际的登录API调用
    return new Promise((resolve, reject) => {
      // 模拟登录成功
      setTimeout(() => {
        const token = 'mock-token'
        commit('SET_TOKEN', token)
        auth.setToken(token)
        resolve()
      }, 500)
    })
  },
  
  // 退出登录
  logout({ commit }) {
    // 这里应当调用登出API
    return new Promise((resolve) => {
      // 清除令牌
      commit('SET_TOKEN', '')
      auth.clearAuth()
      // 重置状态
      commit('RESET_STATE')
      resolve()
    })
  },
  
  // 获取用户信息
  getUserInfo({ commit }) {
    // 这里应当从API获取用户信息
    return new Promise((resolve) => {
      setTimeout(() => {
        // 模拟用户信息
        const userInfo = {
          name: '张医生',
          position: '主治医师',
          department: '神经外科',
          avatar: 'https://randomuser.me/api/portraits/men/85.jpg',
          id: 'U10001',
          email: 'zhangys@hplus.com',
          mobile: '13800138000'
        }
        
        // 模拟用户统计
        const stats = {
          todo: 12,
          assets: 5,
          requests: 3
        }
        
        // 模拟权限列表
        const permissions = ['asset:view', 'asset:apply', 'finance:view', 'todo:view', 'todo:approve']
        
        commit('SET_USER_INFO', userInfo)
        commit('SET_USER_STATS', stats)
        commit('SET_PERMISSIONS', permissions)
        
        resolve({ userInfo, permissions })
      }, 500)
    })
  },
  
  // 更新用户信息
  updateUserInfo({ commit }, userInfo) {
    // 这里应当调用更新用户信息API
    return new Promise((resolve) => {
      setTimeout(() => {
        commit('SET_USER_INFO', { ...state.user, ...userInfo })
        resolve()
      }, 500)
    })
  }
}

const getters = {
  // 用户是否已登录
  isLoggedIn: state => !!state.token,
  // 检查是否有特定权限
  hasPermission: state => permission => {
    return state.permissions.includes(permission)
  },
  // 检查是否有任意一个权限
  hasAnyPermission: state => permissions => {
    return permissions.some(permission => state.permissions.includes(permission))
  },
  // 检查是否有全部权限
  hasAllPermissions: state => permissions => {
    return permissions.every(permission => state.permissions.includes(permission))
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
} 